package com.changgou.goods.service.impl;

import com.changgou.goods.dao.CategoryMapper;
import com.changgou.goods.dao.TemplateMapper;
import com.changgou.goods.pojo.Categories;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Template;
import com.changgou.goods.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private TemplateMapper templateMapper;
    /**
     * 分类列表分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Category> findAll(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Category> categories = (Page<Category>) categoryMapper.selectAll();
        return categories;
    }

    /**
     * 根据上级id查询下级所有数据
     * @param parentId
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Category> findAllByParentId(int parentId, int page, int size) {
        PageHelper.startPage(page,size);
        Example example=new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId",parentId);

        Page<Category> categories = (Page<Category>) categoryMapper.selectByExample(example);
        return categories;
    }

    @Override
    public Page<Template> TemplatefindAll(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Template> Templates = (Page<Template>) templateMapper.selectAll();
        return Templates;
    }

    /**
     * 根据id删除分类
     * @param id
     * fxq
     */
    @Override
    public void deleteById(Integer id) {
        categoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 商品添加
     * @param category
     */
    @Override
    public void add(Category category) {
         categoryMapper.insertSelective(category);
    }

    /**
     * 三级分类查询
     * @return
     */
    @Override
    public List<Categories> findCategories() {
        List<Category> all = categoryMapper.selectAll();
        List<Categories> allList = new ArrayList<>();
        //遍历
        for (Category category : all) {
            //创建对象
            Categories categories = new Categories();
            categories.setId(category.getId());
            categories.setName(category.getName());
            categories.setParentId(category.getParentId());
            allList.add(categories);
        }
        List<Categories> categories = findCategory(0,allList);
        return categories;
    }


    /**
     * 递归方法
     * @param parentId
     * @param list
     * @return
     */
    private List<Categories> findCategory(Integer parentId, List<Categories> list) {
        //创建集合
        List<Categories> categories = new ArrayList<>();
        //遍历
        for (Categories categories1 : list) {
            if(parentId.equals(categories1.getParentId()) ){
                categories1.setChild(findCategory(categories1.getId(),list));
                categories.add(categories1);
            }
        }
        return categories;
    }

    /**
     * 跟据id查询上级分类
     * @param id
     */
    @Override
    public void selectParentId(Integer id) {
       categoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增下级
     * @param category
     */
    @Override
    public void addSubordinate(Category category) {
        categoryMapper.insertSelective(category);
    }

}
